var iframe = {};
(function(){
	iframe.InaAutoSiteAd = {
	iframeid:123, //弹出框的id
	urlparam:'',  //url参数
	num:0,    //弹出框的个数
	BoxWin: new Array(),
	ina_uid:0,
	ina_div:'',
	ina_pv_img:'',
	ina_img_width:1,
	ina_img_height:1,
	ina_img_border:0,
	ina_div_adid:65388,
	ina_ad_aid:0,
	ina_domain:'news18a.com',
	ina_msgshow:1,
	ina_boxW:242,//图片外框宽度
	ina_boxH:350,//图片外框高度
	ina_date_time:new Date().getTime() + Math.random(),
	
	onload:function(){
		ina_div = '<div id="Ina_ad_show_'+this.ina_div_adid+'"></div>';
		document.write(ina_div);	
	},
	Browser:{
		ie:/msie/.test(window.navigator.userAgent.toLowerCase()),
		moz:/gecko/.test(window.navigator.userAgent.toLowerCase()),
		opera:/opera/.test(window.navigator.userAgent.toLowerCase()),
		safari:/safari/.test(window.navigator.userAgent.toLowerCase())

	},
	init:function(iframeid,urlparam,iframe_url,width,height){
		//初始化div
		//this.onload();	
		
		//弹出窗口的id
		this.iframeid = iframeid || this.iframeid;
		
		//打开窗口的url参数
		this.urlparam = urlparam;
		
		//痰喘url
		this.iframe_url = iframe_url;	

        //弹出框的宽和高
        this.ina_boxW = width || this.ina_boxW;
        this.ina_boxH = height || this.ina_boxH;

		//弹窗展示
		if(this.ina_msgshow>0){
			this.createBox();
		}

		this.num ++; //弹出框的个数加一
		//this._log();
	},
	bind: function() { //绑定窗口滚动条与大小变化事件
		var that = this,st, rt;
		window.onscroll = function() {
			if( !!window.ActiveXObject && !window.XMLHttpRequest ){
				clearTimeout(st);
				clearTimeout(that.timer2);
				that.setOpacity(0);
				st = setTimeout(function() {
					for (var i=0; i<that.num; i++){
						that.BoxWin[i].style.top = that.getY(i).top;
					}
					that.show();
				},500);
			}
	};
	window.onresize = function(){
			if (!!window.ActiveXObject && !window.XMLHttpRequest) {
				clearTimeout(rt);
				rt = setTimeout(function(){
					for ( var i=0; i<that.num; i++){
						that.BoxWin[i].style.top = that.getY(i).top
					}
				}, 100);
			}
		}
    },
	show: function() { //渐显
        clearInterval(this.timer2);
        var that = this,
        fx = this.fx(0, 100, 0.1),
        t = 0;
        this.timer2 = setInterval(function() {
            t = fx();
            that.setOpacity(t[0]);
            if (t[1] == 0) {
                clearInterval(that.timer2)
            }
        },
        10);
    },
	fx: function(a, b, c) { //缓冲计算
        var cMath = Math[(a - b) > 0 ? "floor": "ceil"],c = c || 0.1;
        return function() {
            return [a += cMath((b - a) * c), a - b]
        }
    },
	setOpacity: function(x) { //设置透明度
        var v = x >= 100 ? '': 'Alpha(opacity=' + x + ')';
		for (var i=0; i<this.num; i++){
			this.BoxWin[i].style.visibility = x <= 0 ? 'hidden': 'visible'; //IE有绝对或相对定位内容不随父透明度变化的bug
			this.BoxWin[i].style.filter = v;
			this.BoxWin[i].style.opacity = x / 100;
		}

    },
	getY: function(i) { //计算移动坐标
        var d = document,b = document.body,e = document.documentElement;
        var s = Math.max(b.scrollTop, e.scrollTop);
        var h = /BackCompat/i.test(document.compatMode) ? b.clientHeight: e.clientHeight;
		
        var h2 = this.BoxWin[i].offsetHeight;
        return {
            foot: s + h + h2 + 2 + 'px',
            top: s + h - h2 - 2 + 'px'
        }
    },
	moveTo: function(y) { //移动动画
        clearInterval(this.timer);
        var that = this;
		var moveTopNum=-that.ina_boxH;
        this.timer = setInterval(function() {
			moveTopNum+=10; // 底部边距
			for (var i=0; i<that.num; i++){
				that.BoxWin[i].style.bottom =  moveTopNum +'px';
			}
            if (moveTopNum >= 0) {
                clearInterval(that.timer);
				that.bind();
            }
        },20);
		return this;
    },
	createBox:function(){
		this.BoxWin.push(document.createElement('div'));
		var num = this.num;
		this.BoxWin[num].style.zIndex = 1000000;
        this.BoxWin[num].style.width = this.ina_boxW+"px";
        this.BoxWin[num].style.height =  this.ina_boxH+"px";
		this.BoxWin[num].style.bottom = - this.ina_boxH+"px";
        this.BoxWin[num].style.right = this.num*(this.ina_boxW+5)+"px";
        if( !!window.ActiveXObject && !window.XMLHttpRequest ){
            this.BoxWin[num].style.position="absolute";
        }else{
            this.BoxWin[num].style.position="fixed";
        }
        this.BoxWin[num].id = "iframe-box-"+this.iframeid;
        //this.BoxWin.innerHTML = '<a href="'+this.linkURL+'" target="'+this.targetClass+'"><img src="'+this.imgSrc+'" border="0" alt="'+this.imgAlt+'" title="'+this.imgAlt+'"></a><iframe src="" width="'+this.boxW+'" height="'+this.boxH+'" style="position:absolute;top:0; left:0; border:0 none; opacity: 0;-ms-filter: \"alpha(opacity=0)\"; filter: alpha(opacity=0);zoom: 1;"></iframe> ';
		this.BoxWin[num].innerHTML = '<iframe src="'+ this.iframe_url +'?iframeid='+this.iframeid+'&'+this.urlparam+'" width="'+this.ina_boxW+'" height="'+this.ina_boxH+'" scrolling="no" frameborder="no"> </iframe>';
        document.body.appendChild(this.BoxWin[num]);
		var that = this;
		setTimeout(function() { //初始化最先位置
			for ( var i=0; i<that.num ;i++ ){
				that.BoxWin[i].style.display = 'block';
			}
            that.moveTo();
        },1000);
		return this;
	}
}
})(iframe);
